/*
 * str_util.c
 *
 *  Created on: May 8, 2016
 *      Author: Alan Wang
 */
#include "str_util.h"

#include "log.h"

/**
 * copy char str to specified offset
 */
void pstr_copy(char* dst, int dst_pos, char* src, int src_pos, int len) {
    int i;
    for(i = 0;i < len;i++) {
    	*(dst + i + dst_pos) = *(src + i + src_pos);
    }
}

/**
 * print arr in hex
 */
void prt_pstr_in_hex(char* pstr, int len){
	int i;
	for(i = 0;i < len; i++) {
		printf("0x%02X ", *(pstr + i));
	}
	printf("\n");
}

/**
 * convert int to big endian with 4 byte
 */
void int_to_big_endian(char* result, int integer) {
    int i;
    for (i = 0; i < 4; i++) {
        *(result + 4 - 1 -i) = (char) (integer >> 8 * i & 0xFF);
    }
}

/**
 * convert big endian with 4 byte to int
 */
int big_endian_to_int(char* bytes, int len) {
    int iOutcome = 0;
    char bLoop;
    int i;
    for (i = 0; i < len; i++) {
        bLoop = *(bytes + i);
        iOutcome += (bLoop & 0xFF) << (8 * (len - 1-i));
    }
    return iOutcome;
}
